package notify

import (
	"github.com/starmotioncf/common/notify"
	"github.com/starmotioncf/common/notify/feishu"
	notify2 "github.com/starmotioncf/common/notify/notify"
)

const DefaultTopic notify.Topic = "default"
const ErrorTopic notify.Topic = "error"
const OperationTopic notify.Topic = "operation"

type Config struct {
	HookNotice    string `help:"普通消息的告警钩子" default:""`
	HookError     string `help:"error错误的告警钩子" default:""`
	HookOperation string `help:"活动运营消息通知钩子" default:""`
}

type Notify struct {
	client notify.Notify
}

func NewNotify(conf Config) *Notify {
	client := notify2.NewNotify()
	if conf.HookError != "" {
		client.Register(ErrorTopic, feishu.NewFeishuAsync(feishu.Config{Webhook: conf.HookError}))
	}
	if conf.HookNotice != "" {
		client.Register(DefaultTopic, feishu.NewFeishuAsync(feishu.Config{Webhook: conf.HookNotice}))
	}
	if conf.HookOperation != "" {
		client.Register(OperationTopic, feishu.NewFeishuAsync(feishu.Config{Webhook: conf.HookOperation}))
	}
	return &Notify{
		client: client,
	}
}

func (n *Notify) Client() notify.Notify {
	return n.client
}

func (n *Notify) SendMsg(topic notify.Topic, msg notify.Msg) {
	_ = n.client.Send(topic, msg)
}

type NotifyMsg string

func (n NotifyMsg) String() string {
	return string(n)
}
